{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def compress(self, chars):\n",
    "        \"\"\"\n",
    "        :type chars: List[str]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        flips = [(chars[0], 0)] + [(chars[i], i) for i in range(1, len(chars)) if chars[i] != chars[i - 1]] + [(None, len(chars))]\n",
    "        chunks = [(b[0], a[1] - b[1]) for (a, b) in zip(flips[1:], flips)]\n",
    "        compressed = [d for a, b in chunks for d in (a + str(b) if b > 1 else a)]\n",
    "        compressed = ''.join(a + str(b) if b > 1 else a for a, b in chunks)\n",
    "        chars[:len(compressed)] = compressed\n",
    "        return len(compressed)        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.compress([\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"b\",\"b\",\"c\",\"c\",\"c\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
